﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

using DAL;
using CTRL;

namespace GUI_Client
{
    public partial class ReturnBatteries : Form
    {
        private static ReturnBatteries instance;
        private List<Booking> list;
        private CtrlBooking bookCtrl;
        private CtrlBookingItem bookItemCtrl;
        private CtrlBatteryStation batStatCtrl;
        private CtrlBatteryItem batItemCtrl;

        private ReturnBatteries()
        {
            bookCtrl = CtrlBooking.Instance;
            bookItemCtrl = CtrlBookingItem.Instance;
            batStatCtrl = CtrlBatteryStation.Instance;
            batItemCtrl = CtrlBatteryItem.Instance;
            this.list = bookCtrl.GetAllBookingsOfCustomer(GUIMain.Customer.customerID);

            InitializeComponent();
            ShowTable();
        }

        public static ReturnBatteries Instance
        {
            get
            {
                if (instance == null)
                    instance = new ReturnBatteries();
                
                return instance;
            }
        }

        private void ShowTable()
        {
            table.Rows.Clear();
            foreach (Booking book in list)
            {
                List<BookingItem> items = bookItemCtrl.GetBookingItemsFromBooking(book.bookingID).ToList();
                foreach (BookingItem item in items)
                {
                    BatteryItem bi = batItemCtrl.GetBatteryItem(item.batteryID);
                    BatteryStation bs = batStatCtrl.GetBatteryStationByID(bi.stationID);
                    if (bi.state == BatteryState.taken.ToString())
                        table.Rows.Add(book.bookingID, item.batteryID, bs.name);
                }
            }
            if (table.Rows.Count == 0)
            {
                label1.Visible = true;
                returnBtn.Visible = false;
                stationNameLbl.Visible = false;
                stationNameTxtBox.Visible = false;
            }
        }

        private void returnBtn_Click(object sender, EventArgs e)
        {
            if (batStatCtrl.FindBatteryCenter(stationNameTxtBox.Text.ToString()) != null)
            {
                BatteryItem batItem = batItemCtrl.GetBatteryItem(int.Parse(table.CurrentRow.Cells[1].Value.ToString()));
                batItemCtrl.UpdateBatteryItem(batItem.batteryID, batItem.modelID, stationNameTxtBox.Text.ToString(),
                    BatteryState.available);
                MessageBox.Show("You have successfully returned the battery", "Succeeded",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                ShowTable();
                stationNameTxtBox.Clear();
            }
            else MessageBox.Show("This battery station does not exist", "Failed",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void back_Click(object sender, EventArgs e)
        {
            GUIBookings main = GUIBookings.Instance;
            main.Show();

            this.Hide();
        }
    }
}